package cn.hsa.fqz.monitor.service.impl;

import cn.hsa.fqz.monitor.controller.vo.*;
import cn.hsa.fqz.monitor.dao.*;
import cn.hsa.fqz.monitor.entity.*;
import cn.hsa.fqz.monitor.service.MonitorEntityService;
import cn.hsa.fqz.monitor.service.dto.MonitorBussEntityDTO;
import cn.hsa.fqz.monitor.service.dto.MonitorBussEntityRtDTO;
import cn.hsa.fqz.monitor.service.dto.MonitorEntityInstanceDTO;
import cn.hsa.fqz.task.dao.SysCodeMapper;
import cn.hsa.fqz.task.entity.SysCode;
import cn.hsa.fqz.task.service.IDataFluxService;
import cn.hsa.hsaf.core.framework.util.PageResult;
import cn.hsa.hsaf.core.framework.web.exception.BusinessException;
import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @ClassName MonitorEntityServiceImpl
 * @Author Liu.Jun
 * @Create 2023-12-06 15:01
 * @Version 1.0.0
 */
@Service
@Slf4j
public class MonitorEntityServiceImpl implements MonitorEntityService {

    @Value("${zmjk.monitor.table.prefix}")
    private String tablePrefix;

    @Autowired
    private TMonitorEntityMapper tMonitorEntityMapper;

    @Autowired
    private TMonitorTagMapper tMonitorTagMapper;

    @Autowired
    private TMonitorModelDictMapper tMonitorModelDictMapper;

    @Autowired
    private TMonitorEntityDimensionMapper tMonitorEntityDimensionMapper;

    @Autowired
    private TMonitorEntityDimensionPropertiesMapper tMonitorEntityDimensionPropertiesMapper;

    @Autowired
    private TMonitorModelMapper tMonitorModelMapper;

    @Autowired
    private SysCodeMapper sysCodeMapper;

    @Autowired
    private IDataFluxService dataFluxService;

    @Autowired
    private TMonitorEntityAdmdvsMapper tMonitorEntityAdmdvsMapper;

    @Autowired
    private TMonitorBussTypeMapper tMonitorBussTypeMapper;

    @Autowired
    private TMonitorEntityPropertiesMapper tMonitorEntityPropertiesMapper;

    @Autowired
    private TMonitorEntityExecuteSqlPropertiesMapper tMonitorEntityExecuteSqlPropertiesMapper;

    @Autowired
    private TMonitorBussEntityRtMapper tMonitorBussEntityRtMapper;

    @Autowired
    private TMonitorDimensionMapper tMonitorDimensionMapper;

    @Autowired
    private TMonitorEntityExecuteSqlMapper tMonitorEntityExecuteSqlMapper;

    @Override
    public List<TMonitorEntity> getAllEntity() {
        return tMonitorEntityMapper.getAllEntity();
    }

    @Override
    public TMonitorEntity getEntityByCode(String code) {
        return tMonitorEntityMapper.getEntityByCode(code);
    }

    @Override
    public List<TMonitorModelDict> getModelDictByEnityCode(String entityCode) {
        return tMonitorModelDictMapper.selectByEntityCode(entityCode);
    }

    @Override
    public MonitorEntityInstanceDTO getEntityInstinceByCode(String host,Integer port,String passwd,TagMonitorQueryVO vo) {
        MonitorEntityInstanceDTO result = new MonitorEntityInstanceDTO();
        List<TMonitorEntityDimensionProperties> entityProperties = tMonitorEntityDimensionPropertiesMapper.getEntityPropertiesByEntityDimensionId(vo.getId());
        List<String> entityCodeProps = entityProperties.stream().map(TMonitorEntityDimensionProperties::getPropCode).collect(Collectors.toList());
        /*String selectSql = genEntitySelectSql(entityCodeProps,vo.getEntityCode(),vo.getQueryType(),entity.getSerialField(),vo.getSubjectCode());
        List<Map<String, Object>> entityinstance = dataFluxService.getTaskSqlExecuteList(host,port,passwd, selectSql);*/
        List<TMonitorTag> tagList = tMonitorTagMapper.selectList(vo.getEntityCode());
        List<Map<String,String>> tagResult = Lists.newArrayList();
        List<TMonitorEntityAdmdvs> admdvsList = tMonitorEntityAdmdvsMapper.selectByEntityCode(vo.getEntityCode());
        if(CollectionUtil.isNotEmpty(tagList)){
            tagList.forEach( e-> {
                Map<String, String> modelMap = new TreeMap<>();
                modelMap.put("value",e.getTagName());
                modelMap.put("code", e.getTagCode());
                tagResult.add(modelMap);
            });
        }
        List<TMonitorModelDict> dicts = tMonitorModelDictMapper.selectByEntityCodeAndModelCode(vo.getEntityCode(), vo.getModelCode());
        Map<String,List<Map<String,String>>> dictList = genMonitorDict(dicts);
        List<TMonitorModel> modelList = tMonitorModelMapper.getModelByBussCode(vo.getEntityCode());
        List<Map<String,String>> modelResult = Lists.newArrayList();
        if(CollectionUtil.isNotEmpty(modelList)){
            modelList.forEach( e-> {
                Map<String, String> modelMap = new TreeMap<>();
                modelMap.put("value",e.getModelName());
                modelMap.put("code", e.getModelCode());
                modelResult.add(modelMap);
            });
        }
        result.setModelList(modelResult);
        result.setTagList(tagResult);
        //result.setEntity(entityinstance);
        result.setAdmdvsList(admdvsList);
        result.setDictMap(dictList);
        return result;
    }

    @Override
    public Boolean insertEntityAndProperties(TMonitorEntityVO vo) {
        List<String> bussCodes = vo.getBussCodes();
        TMonitorEntity entity = new TMonitorEntity();
        BeanUtils.copyProperties(vo, entity);
        entity.setStatus(1);
        tMonitorEntityMapper.insertSelective(entity);
        if(CollectionUtil.isNotEmpty(bussCodes)) {
            bussCodes.forEach(e -> {
                TMonitorBussEntityRt rt = new TMonitorBussEntityRt();
                rt.setBussCode(e);
                rt.setEntityId(entity.getId());
                rt.setEntityCode(entity.getEntityCode());
                rt.setIsDefault(0);
                tMonitorBussEntityRtMapper.insertSelective(rt);
            });
        }
        return true;
    }

    @Override
    public Boolean deleteEntityAndProperties(String entityCode,String bussType) {
        tMonitorEntityPropertiesMapper.deleteByEntityCodeAndBussType(entityCode,bussType);
        TMonitorEntity entity = tMonitorEntityMapper.getEntityByCode(entityCode);
        tMonitorEntityMapper.deleteByPrimaryKey(entity.getId());
        return true;
    }

    @Override
    public Boolean updateEntityAndProperties(TMonitorEntityVO vo) {
        List<TMonitorEntityPropertiesVO> propertiesVos = vo.getPropertiesVOS();
        TMonitorEntity entity = new TMonitorEntity();
        BeanUtils.copyProperties(vo, entity);
        tMonitorEntityMapper.updateByPrimaryKeySelective(entity);
        if(CollectionUtil.isNotEmpty(propertiesVos)) {
            propertiesVos.forEach(e -> {
                TMonitorEntityProperties properties = new TMonitorEntityProperties();
                BeanUtils.copyProperties(e, properties);
                tMonitorEntityPropertiesMapper.updateByPrimaryKeySelective(properties);
            });
        }
        return true;
    }

    @Override
    public TMonitorEntityVO selectEntityAndProperties(String entityCode,String bussCode) {
        TMonitorEntity entity = tMonitorEntityMapper.getEntityByCode(entityCode);
        List<TMonitorEntityProperties> properties = tMonitorEntityPropertiesMapper.getEntityPropertiesByEntityCodeAndBussCode(entityCode,bussCode);
        TMonitorEntityVO vo = new TMonitorEntityVO();
        BeanUtils.copyProperties(entity,vo);
        if(CollectionUtil.isNotEmpty(properties)){
            List<TMonitorEntityPropertiesVO> propertiesVOS = Lists.newArrayList();
            for (TMonitorEntityProperties property : properties) {
                TMonitorEntityPropertiesVO propertiesVO = new TMonitorEntityPropertiesVO();
                BeanUtils.copyProperties(property,propertiesVO);
                propertiesVOS.add(propertiesVO);
            }
            vo.setPropertiesVOS(propertiesVOS);
        }
        return vo;
    }

    @Override
    public PageResult<TMonitorEntityVO> selectPageEntityAndProperties(TMonitorConfigQueryVO vo) {
        if (vo.getCondition() == null) {
            return null;
        }
        Page<TMonitorEntityVO> page =  PageHelper.startPage(vo.getPage().getPageNum(), vo.getPage().getPageSize());
        List<TMonitorEntityVO> list = tMonitorEntityMapper.selectDataPageByCondition(vo.getCondition());
        for (TMonitorEntityVO entity: list) {
            List<TMonitorEntityProperties> properties = tMonitorEntityPropertiesMapper.getEntityPropertiesByEntityCodeAndBussCode(entity.getEntityCode(),entity.getBussCode());
            List<TMonitorEntityPropertiesVO> propertiesVOS = Lists.newArrayList();
            properties.forEach(e -> {
                TMonitorEntityPropertiesVO propertiesVO = new TMonitorEntityPropertiesVO();
                BeanUtils.copyProperties(e,propertiesVO);
                propertiesVOS.add(propertiesVO);
            });
            entity.setPropertiesVOS(propertiesVOS);
        }
        PageResult<TMonitorEntityVO> result = new PageResult<>();
        result.setData(list);
        result.setPageNum(page.getPageNum());
        result.setPageSize(page.getPageSize());
        result.setPages(page.getPages());
        result.setRecordCounts((int)page.getTotal());
        return result;
    }

    @Override
    public List<TMonitorBussType> getAllBussType() {
        return tMonitorBussTypeMapper.getAllBussType();
    }

    @Override
    public List<MonitorBussEntityDTO> getEntityByBussCode(String bussCode) {
        List<String> entityCodes = tMonitorBussTypeMapper.selectEntityCodeByBussCode(bussCode);
        List<MonitorBussEntityRtDTO> rtDTOS = tMonitorEntityMapper.selectBussEntityRtByEntityCodes(entityCodes);
        List<TMonitorEntity> entityList = tMonitorEntityMapper.getEntityByCodes(entityCodes);
        List<MonitorBussEntityDTO> result = Lists.newArrayList();
        for (TMonitorEntity entity:entityList) {
            MonitorBussEntityDTO dto = new MonitorBussEntityDTO();
            BeanUtils.copyProperties(entity,dto);
            Optional<MonitorBussEntityRtDTO> optional =  rtDTOS.stream().filter(e -> e.getEntityCode().equals(entity.getEntityCode())).findFirst();
            if(optional.isPresent()){
                MonitorBussEntityRtDTO rtDTO = optional.get();
                dto.setIsDefault(rtDTO.getIsDefault());
            }else{
                dto.setIsDefault(0);
            }
            result.add(dto);
        }
        return result;
    }

    @Override
    public Boolean deleteEntityProperties(Integer entityPropId) {
        return tMonitorEntityDimensionPropertiesMapper.deleteByPrimaryKey(entityPropId)>0;
    }

    @Override
    public Boolean addSyncMonitoryEntityProperties(String ckHost, Integer ckPort, String ckPasswd, Integer entityId,String bussType) {
        TMonitorEntity entity = tMonitorEntityMapper.selectByPrimaryKey(entityId);
        String tableName = tablePrefix +bussType+"_"+entity.getEntityCode();
        String sql = "select * from " + tableName + " limit 1";
        List<String> columns = dataFluxService.getColumns(ckHost, ckPort, ckPasswd, sql);
        if(CollectionUtil.isEmpty(columns)){
            throw new BusinessException(404,"当前配置表: 【"+tableName+"】不存在");
        }
        for(int i=0;i<columns.size();i++){
            TMonitorEntityProperties properties = new TMonitorEntityProperties();
            properties.setEntityId(entity.getId());
            properties.setEntityCode(entity.getEntityCode());
            properties.setPropCode(columns.get(i));
            properties.setPreview(1);
            properties.setStatus(1);
            properties.setSortBy(i);
            properties.setIsEncrypt(0);
            properties.setCategory(3);
            properties.setIsVisable(1);
            properties.setIsDefault(0);
            properties.setIsAdmdvsField(0);
            tMonitorEntityPropertiesMapper.insertSelective(properties);
        }
        return true;
    }

    @Override
    public Boolean addSyncMonitoryEntityDimensionProperties(String ckHost, Integer ckPort, String ckPasswd, Integer entityDimensionId) {
        TMonitorEntityDimension entityDimension = tMonitorEntityDimensionMapper.selectByPrimaryKey(entityDimensionId);
        String tableName = entityDimension.getTableName();
        String sql = "select * from " + tableName + " limit 1";
        List<String> columns = dataFluxService.getColumns(ckHost, ckPort, ckPasswd, sql);
        if(CollectionUtil.isEmpty(columns)){
            throw new BusinessException(404,"当前配置表: 【"+tableName+"】不存在");
        }
        for(int i=0;i<columns.size();i++){
            TMonitorEntityDimensionProperties properties = new TMonitorEntityDimensionProperties();
            properties.setEntityDimensionId(entityDimension.getId());
            properties.setPropCode(columns.get(i));
            properties.setPreview(1);
            properties.setStatus(1);
            properties.setSortBy(i);
            properties.setIsEncrypt(0);
            properties.setCategory(3);
            properties.setIsVisable(1);
            properties.setIsDefault(0);
            properties.setIsAdmdvsField(0);
            tMonitorEntityDimensionPropertiesMapper.insertSelective(properties);
        }
        return true;
    }

    @Override
    public Boolean addSyncMonitoryEntityExecSqlProperties(String ckHost, Integer ckPort, String ckPasswd, Integer entityExecSqlId) {
        TMonitorEntityExecuteSql sql = tMonitorEntityExecuteSqlMapper.selectByPrimaryKey(entityExecSqlId);
        String finalSql = "select * from (" + sql.getExecSql() + ") limit 1";
        List<String> columns = dataFluxService.getColumns(ckHost, ckPort, ckPasswd, finalSql);
        if(CollectionUtil.isEmpty(columns)){
            throw new BusinessException(404,"当前配置语句: 【"+sql.getTableName()+"】对应表不存在");
        }
        for(int i=0;i<columns.size();i++){
            TMonitorEntityExecuteSqlProperties properties = new TMonitorEntityExecuteSqlProperties();
            properties.setExecuteSqlId(entityExecSqlId);
            properties.setPropCode(columns.get(i));
            properties.setPreview(1);
            properties.setStatus(1);
            properties.setSortBy(i);
            properties.setIsEncrypt(0);
            properties.setCategory(3);
            properties.setIsVisable(1);
            properties.setIsDefault(0);
            properties.setIsAdmdvsField(0);
            tMonitorEntityExecuteSqlPropertiesMapper.insertSelective(properties);
        }
        return true;
    }

    @Override
    public Boolean insertBuss(MonitorBussTypeVO vo) {
        TMonitorBussType po = new TMonitorBussType();
        BeanUtils.copyProperties(vo,po);
        tMonitorBussTypeMapper.insertSelective(po);
        return true;
    }

    @Override
    public Boolean updateBuss(MonitorBussTypeVO vo) {
        TMonitorBussType po = new TMonitorBussType();
        BeanUtils.copyProperties(vo,po);
        tMonitorBussTypeMapper.updateByPrimaryKeySelective(po);
        return true;
    }

    @Override
    public Boolean deleteBuss(Integer bussId) {
        tMonitorBussTypeMapper.deleteByPrimaryKey(bussId);
        return true;
    }

    @Override
    public PageResult<MonitorBussTypeVO> selectPageBuss(TMonitorConfigQueryVO vo) {
        if (vo.getCondition() == null) {
            return null;
        }
        Page<MonitorBussTypeVO> page =  PageHelper.startPage(vo.getPage().getPageNum(), vo.getPage().getPageSize());
        List<TMonitorBussType> list = tMonitorBussTypeMapper.selectPageByCondition(vo.getCondition());
        List<MonitorBussTypeVO> data = Lists.newArrayList();
        for (TMonitorBussType entity: list) {
            MonitorBussTypeVO entityVO = new MonitorBussTypeVO();
            BeanUtils.copyProperties(entity,entityVO);
            data.add(entityVO);
        }
        PageResult<MonitorBussTypeVO> result = new PageResult<>();
        result.setData(data);
        result.setPageNum(page.getPageNum());
        result.setPageSize(page.getPageSize());
        result.setPages(page.getPages());
        result.setRecordCounts((int)page.getTotal());
        return result;
    }

    @Override
    public Boolean insertDimension(DimensionVO vo) {
        TMonitorDimension dimension= new TMonitorDimension();
        BeanUtils.copyProperties(vo,dimension);
        return tMonitorDimensionMapper.insertSelective(dimension)>0;
    }

    @Override
    public Boolean updateDimension(DimensionVO vo) {
        TMonitorDimension dimension= new TMonitorDimension();
        BeanUtils.copyProperties(vo,dimension);
        return tMonitorDimensionMapper.updateByPrimaryKeySelective(dimension)>0;
    }

    @Override
    public Boolean deleteDimension(Integer dimensionId) {
        return tMonitorDimensionMapper.deleteByPrimaryKey(dimensionId)>0;
    }

    @Override
    public List<TMonitorDimension> selectDimension(String entityCode) {
        List<TMonitorDimension> ls = tMonitorDimensionMapper.selectByEntityCode(entityCode);
        return ls;
    }

    @Override
    public List<TMonitorEntityProperties> selectEntityProperties(String entityCode, String bussCode) {
        return tMonitorEntityPropertiesMapper.getEntityPropertiesByEntityCodeAndBussCode(entityCode,bussCode);
    }

    @Override
    public PageResult<TMonitorDimension> selectPageDimension(TMonitorConfigQueryVO vo) {
        Page<TMonitorDimension> page =  PageHelper.startPage(vo.getPage().getPageNum(), vo.getPage().getPageSize());
        List<TMonitorDimension> list = tMonitorDimensionMapper.selectPageByCondition(vo.getCondition());
        PageResult<TMonitorDimension> result = new PageResult<>();
        result.setData(list);
        result.setPageNum(page.getPageNum());
        result.setPageSize(page.getPageSize());
        result.setPages(page.getPages());
        result.setRecordCounts((int)page.getTotal());
        return result;
    }

    @Override
    public List<TMonitorEntityDimensionProperties> selectEntityDimensionProperties(String entityCode, String tableName) {
        return tMonitorEntityDimensionPropertiesMapper.selectEntityDimensionPropertiesByEntityCodeAndTableName(entityCode,tableName);
    }

    @Override
    public Boolean insertEntityDimension(EntityDimensionVO vo) {
        TMonitorEntityDimension po = new TMonitorEntityDimension();
        BeanUtils.copyProperties(vo,po);
        tMonitorEntityDimensionMapper.insertSelective(po);
        return true;
    }

    @Override
    public Boolean updateEntityDimension(EntityDimensionVO vo) {
        TMonitorEntityDimension po = new TMonitorEntityDimension();
        BeanUtils.copyProperties(vo,po);
        tMonitorEntityDimensionMapper.updateByPrimaryKeySelective(po);
        List<EntityDimensionPropertiesVO> dimensionPropertiesVOS = vo.getEntityDimensionPropertiesVOS();
        if(CollectionUtil.isNotEmpty(dimensionPropertiesVOS)){
            for (EntityDimensionPropertiesVO propertiesVO: dimensionPropertiesVOS) {
                TMonitorEntityDimensionProperties dimension = new TMonitorEntityDimensionProperties();
                BeanUtils.copyProperties(propertiesVO,dimension);
                tMonitorEntityDimensionPropertiesMapper.updateByPrimaryKeySelective(dimension);
            }
        }
        return true;
    }

    @Override
    public PageResult<EntityDimensionVO> selectPageEntityDimension(TMonitorConfigQueryVO vo) {
        Page<EntityDimensionVO> page =  PageHelper.startPage(vo.getPage().getPageNum(), vo.getPage().getPageSize());
        List<TMonitorEntityDimension> list = tMonitorEntityDimensionMapper.selectPageByCondition(vo.getCondition());
        List<EntityDimensionVO> data = Lists.newArrayList();
        for (TMonitorEntityDimension dimension:list) {
            EntityDimensionVO entityDimensionVO = new EntityDimensionVO();
            BeanUtils.copyProperties(dimension,entityDimensionVO);
            List<TMonitorEntityDimensionProperties> props = tMonitorEntityDimensionPropertiesMapper.selectEntityDimensionPropertiesByEntityCodeAndTableName(dimension.getEntityCode(), dimension.getTableName());
            List<EntityDimensionPropertiesVO> entityDimensionPropertiesVOS = Lists.newArrayList();
            if(CollectionUtil.isNotEmpty(props)){
                for (TMonitorEntityDimensionProperties prop:props) {
                    EntityDimensionPropertiesVO vo1 = new EntityDimensionPropertiesVO();
                    BeanUtils.copyProperties(prop,vo1);
                    entityDimensionPropertiesVOS.add(vo1);
                }
            }
            entityDimensionVO.setEntityDimensionPropertiesVOS(entityDimensionPropertiesVOS);
            data.add(entityDimensionVO);
        }
        PageResult<EntityDimensionVO> result = new PageResult<>();
        result.setData(data);
        result.setPageNum(page.getPageNum());
        result.setPageSize(page.getPageSize());
        result.setPages(page.getPages());
        result.setRecordCounts((int)page.getTotal());
        return result;
    }

    @Override
    public Boolean deleteEntityDimension(Integer entityDimensionId) {
        tMonitorEntityDimensionMapper.deleteByPrimaryKey(entityDimensionId);
        return true;
    }

    @Override
    public Boolean insertExcuteSql(EntityExcuteSqlVO vo) {
        TMonitorEntityExecuteSql sql = new TMonitorEntityExecuteSql();
        BeanUtils.copyProperties(vo,sql);
        tMonitorEntityExecuteSqlMapper.insertSelective(sql);
        return true;
    }

    @Override
    public Boolean updateExcuteSql(EntityExcuteSqlVO vo) {
        TMonitorEntityExecuteSql sql = new TMonitorEntityExecuteSql();
        BeanUtils.copyProperties(vo,sql);
        tMonitorEntityExecuteSqlMapper.updateByPrimaryKeySelective(sql);
        return true;
    }

    @Override
    public PageResult<EntityExcuteSqlVO> selectPageExcuteSql(TMonitorConfigQueryVO vo) {
        Page<EntityExcuteSqlVO> page =  PageHelper.startPage(vo.getPage().getPageNum(), vo.getPage().getPageSize());
        List<TMonitorEntityExecuteSql> list = tMonitorEntityExecuteSqlMapper.selectPageByCondition(vo.getCondition());
        List<EntityExcuteSqlVO> data = Lists.newArrayList();
        for (TMonitorEntityExecuteSql executeSql:list) {
            EntityExcuteSqlVO entityDimensionVO = new EntityExcuteSqlVO();
            BeanUtils.copyProperties(executeSql,entityDimensionVO);
            List<TMonitorEntityExecuteSqlProperties> props = tMonitorEntityExecuteSqlPropertiesMapper.selectByTableName(executeSql.getTableName());
            List<EntityExcuteSqlPropertiesVO> entityDimensionPropertiesVOS = Lists.newArrayList();
            if(CollectionUtil.isNotEmpty(props)){
                for (TMonitorEntityExecuteSqlProperties prop:props) {
                    EntityExcuteSqlPropertiesVO vo1 = new EntityExcuteSqlPropertiesVO();
                    BeanUtils.copyProperties(prop,vo1);
                    entityDimensionPropertiesVOS.add(vo1);
                }
            }
            entityDimensionVO.setEntityExcuteSqlPropertiesVOS(entityDimensionPropertiesVOS);
            data.add(entityDimensionVO);
        }
        PageResult<EntityExcuteSqlVO> result = new PageResult<>();
        result.setData(data);
        result.setPageNum(page.getPageNum());
        result.setPageSize(page.getPageSize());
        result.setPages(page.getPages());
        result.setRecordCounts((int)page.getTotal());
        return result;
    }

    @Override
    public Boolean deleteExcuteSql(Integer sqlId) {
        tMonitorEntityExecuteSqlMapper.deleteByPrimaryKey(sqlId);
        return true;
    }

    @Override
    public List<TMonitorEntityExecuteSqlProperties> selectExecuteSqlAndProperties(Integer sqlId) {
        TMonitorEntityExecuteSql sql = tMonitorEntityExecuteSqlMapper.selectByPrimaryKey(sqlId);
        List<TMonitorEntityExecuteSqlProperties> properties = tMonitorEntityExecuteSqlPropertiesMapper.selectByTableName(sql.getTableName());
        return properties;
    }

    private Map<String, List<Map<String,String>>> genMonitorDict(List<TMonitorModelDict> dicts) {
        Map<String,List<Map<String,String>>> result = Maps.newTreeMap();
        List<String> dictFields = dicts.stream().map(TMonitorModelDict::getDictCode).collect(Collectors.toList());
        List<SysCode> dictList = sysCodeMapper.selectListByFields(dictFields);
        Map<String, List<SysCode>> codeMaps = dictList.stream().collect(Collectors.groupingBy(SysCode::getField));
        for (String dict:dictFields) {
            List<Map<String,String>> ls = Lists.newArrayList();
            List<SysCode> tmpCodeLs = codeMaps.get(dict);
            for (SysCode code:tmpCodeLs) {
                Map<String,String> mmp = new TreeMap<>();
                mmp.put("value", code.getKey());
                mmp.put("code", code.getValue());
                ls.add(mmp);
            }
            result.put(dict, ls);
        }
        return result;
    }

    private String genEntitySelectSql(List<String> entityCodeProps, String entityCode, String queryType,String serialField,List<String> value) {
        StringBuffer result = new StringBuffer();
        StringBuffer buffer = new StringBuffer("select ");
        for (String prop:entityCodeProps) {
            buffer.append(prop).append(",");
        }
        String fieldsVal = genFieldsSql(value);
        result.append(buffer.toString().substring(0, buffer.toString().length() - 1)).append(" from ").append(getTableNameByType(queryType))
                .append(" where ").append(serialField).append(" in (").append(fieldsVal).append(") limit 1");
        return result.toString();
    }

    private String genFieldsSql(List<String> value) {
        StringBuffer buffer = new StringBuffer();
        for (String str:value) {
            buffer.append("'").append(str).append("',");
        }
        return buffer.substring(0,buffer.length()-1).toString();
    }

    private String getTableNameByType(String type){
        StringBuffer buffer = new StringBuffer(" and timetype = '"+type+"'");
        return buffer.toString();
    }
}
